package com.wohuijituan.whcsmerchant.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.wohuijituan.whcsmerchant.R;
import com.wohuijituan.whcsmerchant.activity.GoodsManageActivity;
import com.wohuijituan.whcsmerchant.bean.GoodsCategoryBCData;
import com.wohuijituan.whcsmerchant.bean.GoodsCategoryData;
import com.wohuijituan.whcsmerchant.myview.MyGridViewInListView;
import com.wohuijituan.whcsmerchant.utils.ExtraName;

import java.util.ArrayList;
import java.util.List;

/**
 * 说明：
 * 作者：朱世元
 * 时间： 2016/8/16 16:34
 * 版本：V1.0
 * 修改历史：
 */
public class GoodsCategoryBAdapter extends MyBaseAdapter<GoodsCategoryBCData>{
    public GoodsCategoryBAdapter(Context ctx, List dataList) {
        super(ctx,dataList);
    }
    private static final String TAG = "GoodsCategoryBAdapter";
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder vh;
        if (convertView == null){
            convertView = View.inflate(ctx, R.layout.item_goods_category_b,null);
            vh = new ViewHolder(convertView);
            //初始化GoodsCategoryC
            List<GoodsCategoryData> categoryDatas = new ArrayList<>();
            vh.cAdapter = new GoodsCategoryCAdapter(ctx,categoryDatas);
            //给ViweHolder中的列表适配器建立联系,并设置适配器
            vh.gvGoodsCategoryC.setAdapter(vh.cAdapter);
            vh.gvGoodsCategoryC.setTag(position);
            vh.gvGoodsCategoryC.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position2, long id) {
                    //TODO 得到需要携带的数据,跳转到商品管理操作的界面
                    int position = (int) vh.gvGoodsCategoryC.getTag();
                    Intent intent = new Intent(ctx, GoodsManageActivity.class);
                    String categoryNumber = dataList.get(position).getCategoryCList()
                            .get(position2).getCC_GUID();
                    String categoryName = dataList.get(position).getCategoryCList()
                            .get(position2).getCC_Name();
                    intent.putExtra(ExtraName.CATEGORY_NUMBER, categoryNumber);
                    intent.putExtra(ExtraName.CATEGORY_NAME,categoryName);
                    ctx.startActivity(intent);
                }
            });
            convertView.setTag(vh);
        }else {
            vh = (ViewHolder) convertView.getTag();
            vh.gvGoodsCategoryC.setTag(position);
        }

        vh.cAdapter.replace(getCCategoryList(position));
        vh.tvGoodsCategoryB.setText(dataList.get(position).getCC_Name());

        return convertView;
    }
    /**
     * 得到二级列表下的三级种类列表
     * @param position
     * @return 三级种类列表
     */
    private List<GoodsCategoryData> getCCategoryList(int position) {
        return dataList.get(position).getCategoryCList();
    }
    public class ViewHolder {
        public final TextView tvGoodsCategoryB;
        public final MyGridViewInListView gvGoodsCategoryC;
        public final View root;
        public GoodsCategoryCAdapter cAdapter;
        public ViewHolder(View root) {
            tvGoodsCategoryB = (TextView) root.findViewById(R.id.tv_goods_category_b);
            gvGoodsCategoryC = (MyGridViewInListView) root.findViewById(R.id.gv_goods_category_c);
            this.root = root;
        }
    }
}
